package com.educate.ucenter.rpc.provide.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.educate.common.utils.converter.ListUtils;
import com.educate.common.utils.exception.AppException;
import com.educate.ucenter.dao.AccountDao;
import com.educate.ucenter.model.bo.AccountBO;
import com.educate.ucenter.model.po.Account;
import com.educate.ucenter.rpc.api.UserAccountService;
import lombok.extern.log4j.Log4j2;
import org.springframework.data.domain.Page;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Log4j2
@Service(
        version = "${api.service.version}",
        application = "${dubbo.application.id}",
        protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}"
)
public class AccountServiceImpl implements UserAccountService {

    @Resource AccountDao accountDao;

    @Override
    public void create(AccountBO bo) {
        accountDao.insert(bo);
    }

    @Override
    public void delete(AccountBO bo) {
        accountDao.delete(bo);
    }

    @Override
    public void delete(String userAccountId) {
        accountDao.deleteById(userAccountId);
    }

    @Override
    public AccountBO detail(String userAccountId) {
        Optional<Account> optional = accountDao.findById(userAccountId);
        if (!optional.isPresent()) {
            throw new AppException("不存在的实例");
        }
        return new AccountBO(optional.get());
    }

    @Override
    public AccountBO detail(AccountBO bo) {
//        return detail(bo.getOrganizationId());
        return null;
    }

    @Override
    public void update(AccountBO bo) {
//        Optional<Account> optional = userAccountDao.findById(bo.getUserAccountId());
//        if (!optional.isPresent()) {
//            throw new AppException("不存在的实例");
//        }
//        BeanUtils.copyPropertiesNonEmpty(bo, optional.get());
//        userAccountDao.save(optional.get());
    }

    @Override
    public Page<AccountBO> query(AccountBO bo) {
        return accountDao.query(bo, bo.pageable()).map(AccountBO::new);
    }

    @Override
    public List<AccountBO> list(AccountBO bo) {
        List<Account> result = accountDao.findAll();
        return ListUtils.map(result, AccountBO::new);
    }
}
